﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using Jzy.Core.Common.Basic;
using System.Collections;
using System.Drawing;
using FortuneHeavenCommon.ComMethod;

namespace FortuneHeaven.Admin.Common
{
    public class ImagesManager
    {
        private static string SaveToDisk(HttpPostedFile imgFile, string savePath)
        {
            //文件保存目录路径
            try
            {
                HttpContext context = HttpContext.Current;
                string fileName = imgFile.FileName;
                string fileExt = Path.GetExtension(fileName).ToLower();
                //string savePath = "/upload/CMS/";

                string dirPath = context.Server.MapPath(GlobalVar.WebAppPath+ savePath);
                string newfile = Guid.NewGuid().ToString() + fileExt;
                string newpath = string.Format("{0}{1}", dirPath, newfile);
                imgFile.SaveAs(newpath);
                return newfile;
            }
            catch (Exception exp)
            {
                return "";
            }
        }

        public static bool SavePaperSmall(HttpContext current, int width, int height, string savePath, string newname, ref string error)
        {
            Bitmap bt = null;
            Bitmap small = null;
            Graphics g = null;
            try
            {
                HttpPostedFile imgFile = current.Request.Files[0];
                string dirPath = current.Server.MapPath(GlobalVar.WebAppPath + savePath);
                string newpath = string.Format("{0}{1}", dirPath, newname);
                bt = new Bitmap(imgFile.InputStream);
                small = new Bitmap(width, height);
                g = Graphics.FromImage(small);
                g.Clear(Color.Transparent);
                Rectangle src = new Rectangle(0, 0, bt.Size.Width, bt.Size.Height);
                Rectangle target = new Rectangle(0, 0, small.Width, small.Height);
                g.DrawImage(bt, target, src, GraphicsUnit.Pixel);
                small.Save(newpath);
                return true;
            }
            catch (Exception exp)
            {
                error = "压缩失败";
                return false;
            }
            finally
            {
                if (g != null)
                {
                    g.Dispose();
                }
                if (small != null)
                {
                    small.Dispose();
                }
                if (bt != null)
                {
                    bt.Dispose();
                }
            }
             
        }

        public static bool SaveCMSImg(HttpContext context, ref string name, ref string error)
        {
            return SaveImg(context, CommonPath.CMS, ref name, ref error);
        }

        public static bool SaveEquipImg(HttpContext context,  ref string name, ref string error)
        {
            return SaveImg(context, CommonPath.Equip, ref name, ref error);
        }
        public static bool SaveGameShowImg(HttpContext context, ref string name, ref string error)
        {
            return SaveImg(context, CommonPath.GameShow, ref name, ref error);
        }
        public static bool SaveGiftImg(HttpContext context, ref string name, ref string error)
        {
            return SaveImg(context, CommonPath.Gift, ref name, ref error);
        }

        public static bool SaveWallPaperImg(HttpContext context, ref string name, ref string error)
        {
            if(SaveImg(context, CommonPath.WallPaper, ref name, ref error))
            {
                return SavePaperSmall(context, 98, 82, CommonPath.WallSamllPaper, name, ref error);
            }
            return false;
        }

        public static bool SaveImg(HttpContext context,string path,ref string name,ref string error)
        {

            //定义允许上传的文件扩展名
            String fileTypes = "gif,jpg,jpeg,png,bmp";
            //最大文件大小
            int maxSize = 1000000;

            HttpPostedFile imgFile = context.Request.Files[0];

            String fileName = imgFile.FileName;
            String fileExt = Path.GetExtension(fileName).ToLower();
            ArrayList fileTypeList = ArrayList.Adapter(fileTypes.Split(','));

            if (imgFile.InputStream == null || imgFile.InputStream.Length > maxSize)
            {
                error = "上传文件大小超过限制。";
                return false;
            }

            if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(fileTypes.Split(','), fileExt.Substring(1).ToLower()) == -1)
            {
                error = "上传文件扩展名是不允许的扩展名。";
                return false;
            }

            name = ImagesManager.SaveToDisk(imgFile, path);
            if(string.IsNullOrEmpty(name))
            {
                error = "发生内部错误";
                return false;
            }
            return true;

            
        }
    }
}